<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-col :span="19">
            <el-button
              style="float: left"
              type="primary"
              @click="addGoods()"
              round
            >添加商品</el-button>
          </el-col>
          <el-col :span="5">
            <el-input
              placeholder="请输入内容"
              v-model="queryData.query"
              class="input-with-select"
              @clear="getGoodsList"
              clearable
            >
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
            </el-input>
          </el-col>
        </el-row>
      </div>
      <template>
        <el-table :data="goodsData" stripe style="width: 100%">
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称"></el-table-column>
          <el-table-column prop="goods_price" label="商品价格(元)" width="105px"></el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" width="80px"></el-table-column>
          <el-table-column prop="add_time" label="创建时间" width="170px">
            <template slot-scope="scope">{{scope.row.add_time|dateFormat}}</template>
          </el-table-column>
          <el-table-column label="操作" width="130px">
            <template slot-scope="scope">
              <el-tooltip
                class="item"
                effect="dark"
                content="编辑角色"
                placement="top"
                :enterable="false"
              >
                <el-button
                  @click="getEditUser(scope.row)"
                  type="primary"
                  icon="el-icon-edit"
                  size="mini"
                  round
                  class="r_btns"
                ></el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="删除角色"
                placement="top"
                :enterable="false"
              >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  @click="deleteGoods(scope.row.goods_id)"
                  round
                  class="r_btns"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryData: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      goodsData: []
    };
  },

  created() {
    this.getGoodsList();
  },

  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryData
      });
      console.log(res);
      if (res.meta.status !== 200) return this.$message.error("加载失败");
      this.total = res.data.total;
      this.goodsData = res.data.goods;
    },
    // 分页
    handleSizeChange(val) {
      this.queryData.pagesize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      this.queryData.pagenum = val;
      this.getGoodsList();
    },
    // 删除商品
    deleteGoods(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        console.log(id);
        const { data: res } = await this.$http.delete(`goods/${id}`);
        if (res.meta.status !== 200) return this.$message.error("删除失败");
        this.$message.success("删除成功");
        this.getGoodsList()
      }).catch(()=>{
        this.$message.info('取消删除')
      })
    },
    addGoods(){
      this.$router.push('goods/add')
    }
  }
};
</script>

<style scoped lang='less'>
</style>
